package org.zxp.mail;

import com.sun.mail.util.MailSSLSocketFactory;

import javax.mail.*;
import javax.mail.internet.*;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;

/**
 * @program: effectiveJava
 * @description:
 * @author: X-Pacific zhang
 * @create: 2020-07-22 16:39
 **/
public class Test {

    public static void main(String[] args){
        Properties prop = new Properties();
        //协议
        prop.setProperty("mail.transport.protocol", "smtp");
        //服务器
        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
        //端口
        prop.setProperty("mail.smtp.port", "465");
        //使用smtp身份验证
        prop.setProperty("mail.smtp.auth", "true");
        //使用SSL，企业邮箱必需！
        //开启安全协议
        MailSSLSocketFactory sf = null;
        try {
            sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
        } catch (GeneralSecurityException e1) {
            e1.printStackTrace();
        }
        prop.put("mail.smtp.ssl.enable", "true");
        prop.put("mail.smtp.ssl.socketFactory", sf);
        //
        //获取Session对象
        Session s = Session.getDefaultInstance(prop,new Authenticator() {
            //此访求返回用户和密码的对象
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                PasswordAuthentication pa = new PasswordAuthentication("zhangxinpeng1@newhope.cn", "zxp5083656$");
                return pa;
            }
        });
        //设置session的调试模式，发布时取消
        s.setDebug(true);
        MimeMessage mimeMessage = new MimeMessage(s);
        try {
            mimeMessage.setFrom(new InternetAddress("zhangxinpeng1@newhope.cn","产融平台报警"));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("123549440@qq.com"));
            mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("zxpdt@163.com"));
            //设置主题
            mimeMessage.setSubject("账户密码重置");
            mimeMessage.setSentDate(new Date());
            StringBuffer sb = new StringBuffer();
            sb.append("<h1>").append("标题1").append("</h1>").append("<br/>");
            sb.append("dewiadniwfbnewifbnewfbiewufnbewfiewbfiewufbwiefbewf");
            sb.append("<h1>").append("标题2").append("</h1>").append("<br/>");
            sb.append("dewiadniwfbnewifbnesadadsadadasddswfbiewufnbewfiewbfiewufbwiefbewf");


            Multipart mainPart = new MimeMultipart();
            // 创建一个包含HTML内容的MimeBodyPart
            BodyPart html = new MimeBodyPart();
            // 设置HTML内容
            html.setContent(sb.toString(), "text/html; charset=utf-8");
            mainPart.addBodyPart(html);
            // 将MiniMultipart对象设置为邮件内容
            mimeMessage.setContent(mainPart);
            mimeMessage.saveChanges();
            //发送
            Transport.send(mimeMessage);
        } catch (MessagingException | UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
